<?php
include_once 'catalogs_protoweb.php';

/**
 * @package Protoweb
 * @subpackage controller
 */
class Catalogs extends Catalogs_protoweb {

  public function __construct() {
    parent::__construct();
  }

  public function listing() {
    // Get global instance's objects
    $data['user']   = $this->security();
    $data['client'] = $this->get_client();

    $data['error'] = $this->session->flashdata('error');

    $data['catalogs'] = $data['client']->catalogs;

    $this->smarty_parser->view('catalog_listing.html', $data);
  }

  public function edit($id=0) {
    // Get global instance's objects
    $data['user']   = $this->security();
    $data['client'] = $this->get_client();

    $data['catalog'] = new Catalog($id);

    if ($this->input->server('REQUEST_METHOD') === 'POST') {
      $data = $this->edit_save($data['catalog']->id);
    }
    
    $this->smarty_parser->view('catalog_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    // Get global instance's objects
    $data['user']   = $this->security();
    $data['client'] = $this->get_client();

    $data['catalog'] = new Catalog($id);

    // Assign values to fields.
    $data['catalog']->name        = $this->input->post('name');
    $data['catalog']->description = $this->input->post('description');
    $data['catalog']->active      = TRUE;
    $data['catalog']->created     = date('Y-m-d H:i:s');
    
    //saving attributes
    if ( ! $data['catalog']->save($data['client'])) {
      $data["error"] = $data["catalog"]->error->string;
    }

    // if is new catalog and user select to fill it, we subcribe all client's recipients
    if ($this->input->post('fill')) {
      //TODO
    }

    redirect(base_url()."catalogs/listing/");
  } // end of method edit_save($id)
  
  public function delete($id) {
    $data["catalog"] = new Catalog($id);

    if ( ! $data["catalog"]->delete()) {
      $this->session->set_flashdata('error', $data["catalog"]->error->string);
    }

    redirect(base_url()."catalogs/listing/");
  } // end of method delete($id)

} // class Catalogs

/* End of file catalogs.php */
/* Location: ./system/application/controllers/catalogs.php */